package com.china08.yxyapi.api.manage.tec.teaching.lecture.teachreport;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.esbag.test.YxyEsbagTeachingTestQuesStu;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.esbag.YxyEsbagTeachingTestQuesStuRepository;

/**
 * 老师 ---备授课--授课报告--批阅
 * 
 */
@CrossOrigin
@RestController
public class LectureReportClassCorrectCrtController extends YxyApiBaseController {

    @Autowired
    private YxyEsbagTeachingTestQuesStuRepository yxyEsbagTeachingTestQuesStuRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/testreport/class/corrent/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String id = reqModel.id;
        Assert.notNull(StringUtils.defaultIfBlank(id, null), "授课包ID为空");
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        String quesGroupId = reqModel.quesGroupId;
        String quesId = reqModel.quesId;
        String studentId = reqModel.studentId;
        String classId = reqModel.classId;
        List<String> wrongKnowName = reqModel.knowsNameList;

        List<String> wrongKnowIdList = reqModel.knowsIdList;

        Integer mark = reqModel.mark;

        List<String> exactnessKnowsId = new ArrayList<String>();
        List<String> exactnessKnowsName = new ArrayList<String>();

        RespModel respModel = new RespModel();
        YxyEsbagTeachingTestQuesStu testQuesStus = yxyEsbagTeachingTestQuesStuRepository
                .findByPackIdAndTeacherIdAndSchoolIdAndQuesGroupIdAndQuesIdAndClassIdAndStudentId(id, userId, schoolId,
                        quesGroupId, quesId, classId, studentId);

        testQuesStus.setWrongKnowName(wrongKnowName);
        testQuesStus.setErrorKnowId(wrongKnowIdList);
        testQuesStus.setMark(mark);
        List<String> knowId = testQuesStus.getKnowsId();
        for (String know : knowId) {
            if (!wrongKnowIdList.contains(know)) {
                exactnessKnowsId.add(know);
            }
        }
        List<String> knowName = testQuesStus.getKnowsName();
        for (String name : knowName) {
            if (!wrongKnowName.contains(name)) {
                exactnessKnowsName.add(name);
            }
        }
        testQuesStus.setExactnessKnowId(exactnessKnowsId);
        testQuesStus.setExactnessKnowName(exactnessKnowsName);
        testQuesStus.setIsReadOver(1);
        yxyEsbagTeachingTestQuesStuRepository.save(testQuesStus);

        respModel.setMessage("批改成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 授课包id
        public String id;
        // 试题id
        public String quesId;
        // 题组id
        public String quesGroupId;
        // 学生id
        public String studentId;
        // 学生名称
        public String studentName;
        // 评分
        public Integer mark;
        // 班级ID
        public String classId;
        // 错误知识点
        public List<String> knowsNameList;
        // 错误知识点id
        public List<String> knowsIdList;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
